Java Operators

Operations on variables and values are performed out using operators.

Example

int result = a+b;

Here, addition operation (+) is done on two variables a and b and assigned to variable result.

Operators can be classified into multiple types as below.

  • Arithmetic Operators
  • Assignment Operators
  • Logical Operators
  • Relational Operators
  • Unary Operators
  • Bitwise Operators
  • Ternary Operators
  • Shift Operators

Arithmetic Operators

The arithmetic operators are used to perform addition, subtraction, multiplication, division, modulo operations.

Operator Description Example
+ Adds two values a+b
- Subtracts one value from other value a-b
* Multiplies 2 values a*b
/ Divides one value from other value a/b
% Divides and returns the remainder a%b

Example

public class Main{
      public static void main(String[] args) {
            int x = 100;
            int y = 10;
            System.out.println(x + y);
            System.out.println(x - y);
            System.out.println(x * y);
            System.out.println(x / y);
            System.out.println(x % y);
          }
}

Output

90
1000
10
0

Assignment Operators

The assignment operators are used to assign a value to the variable.

Example

int x = 100;

List of assignment operators are

Operator Description Example
= Assigns values to the left-side operand from the right-side operands. a=10;
+= Adds the right operand and left operand and assign it to left operand a+=b;
-= Subtracts the right operand from left operand and assign it to left operand a*=b;
*= Multiplies the right operand and left operand and assign it to left operand a*=b;
/= Divides the left operand with right operand and assign it to left operand a/=b;
%= It takes modulus using two operands and assigns the result to left operand a%=b;
^= Performs exponential (power) calculation on operators and assign value to the left operand a^=b;

Example

public class Main
{
  public static void main (String[]args)
  {
    int x = 10;
    int y = 20;
    int z;
    System.out.println (z = x);
    System.out.println (x += y);
    System.out.println (x -= y);
    System.out.println (x *= y);
    System.out.println (x /= y);
    System.out.println (x %= y);
    System.out.println (x ^= y);
  }
}

Output

10
30
10
200
10
10
30

Relational Operators

The relational operators are used to compare the values of right and left operands.

Example

x==y

This is used to check whether the values of x and y are equal.

List of relational operators are

Operator Description Example
== Checks whether right and left operands are equal a==b
!= Checks whether right and left operand are not equal a!=b
> Checks whether left operand is greater than right operand a>b
< Checks whether right operand is greater than left operand a<b
>= Checks whether left operand is greater than or equal to right operand a>=b
<= Checks whether right operand is greater than left operand a<=b

Example

public class Main
{
  public static void main (String[] args)
  {
    int x = 100;
    int y = 200;
      System.out.println (x == y);
      System.out.println (x != y);
      System.out.println (x > y);
      System.out.println (x < y);
      System.out.println (x >= y);
      System.out.println (x <= y);
  }
}

Output

false
true
false
true
false
true

Logical Operators

Logic between variables or values is determined by logical operators.

Operator Description Example
&& If both the statements are true, then it returns true. x>y && x>z
|| If either one of the statements is true, then it returns true x>y || x>z
! If an operand is false, it is true !(x>y && x>z)

Example

public class Main
{
  public static void main (String[]args)
  {
    int x = 10, y = 20, z = 30;
    System.out.println (x > y && x > z);
    System.out.println (x > y || x > z);
    System.out.println (!(x > y && x > z));
  }
}

Output

false
false
true

Unary Operators

The unary operators are used to increment or decrement the value of the variable by 1.

Operator Description Example
++ Increments the value of variable by 1. a++ and ++a
-- Decrements the value of variable by 1. a-- and --a

Example

public class Main
{
  public static void main (String[]args)
  {
    int x = 5;
    boolean b = true;
    System.out.println (x++);
    System.out.println (++x);
    System.out.println (x--);
    System.out.println (--x);
  }
}

Output

5
7
7
5

Most Read